"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : Compatibility
Case Name   : 验证位串类型和二进制类型异或操作
Create At   : 2023/01/05
@zou_jialiang0501162243
Description :
        1.位串类型异或
Expect      :
        1.运算成功
History     :
  modified by @yangyixiang at 2023-6-7:研发代码变更，修改用例
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Operator(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.constant = Constant()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.db_name = 'db_operator0002'
        text1 = '--------创建兼容B库；expect:创建成功--------'
        self.log.info(text1)
        create_db = f"drop database if exists {self.db_name};" \
                    f"create database {self.db_name} dbcompatibility='B';" \
                    f"\c {self.db_name}"
        res = self.sh_primary.execut_db_sql(create_db)
        self.log.info(res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, res,
                      '执行失败' + text1)
        msg = f'You are now connected to database "{self.db_name}"'
        self.assertIn(msg, res, '执行失败' + text1)

    def test_operator(self):
        text = '--------step1：位串类型异或；expect：运算成功--------'
        self.log.info(text)
        create_tb = f"select b'1010'^b'0101';" \
                    f"select b'1010'^b'1010';" \
                    f"select b'1010'^b'0010';" \
                    f"select b'1010'^b'0011';" \
                    f"select b'1010'^b'1100';" \
                    f"select b'000'^b'001';" \
                    f"select b'001'^b'000';" \
                    f"select b'000'^b'000';" \
                    f"select b'1010'::bit(4)^b'0101'::bit(4);" \
                    f"select b'1010'::bit(4)^b'1010'::bit(4);" \
                    f"select b'1010'::bit(4)^b'0010'::bit(4);" \
                    f"select b'1010'::bit(4)^b'0011'::bit(4);" \
                    f"select b'1010'::bit(4)^b'1100'::bit(4);" \
                    f"select b'000'::bit^b'001'::bit;" \
                    f"select b'001'::bit^b'000'::bit;" \
                    f"select b'000'::bit^b'000'::bit;"
        res = self.sh_primary.execut_db_sql(create_tb, dbname=self.db_name)
        self.log.info(res)
        self.assertTrue(len(res.splitlines()) > 2)
        val = ['15', '0', '8', '9', '6', '1', '1', '0',
               '15', '0', '8', '9', '6', '1', '1', '0']
        i = 0
        for row in res.splitlines()[2::5]:
            self.assertEqual(row.strip(), val[i])
            i += 1

    def tearDown(self):
        self.log.info('-----清理环境-----')
        text1 = '--------删除兼容B库；expect:删除成功--------'
        self.log.info(text1)
        res0 = self.sh_primary.execut_db_sql(f"drop database if exists "
                                             f"{self.db_name};")
        self.log.info(res0)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, res0,
                      '执行失败' + text1)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
